﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Threading;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.Graphics;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using static System.Net.Mime.MediaTypeNames;
using Tesseract;
using SQLiteHelper;
namespace TestConsole
{
    class Program
    {
        private delegate void optionDel();
        static void Main(string[] args)
        {
            SqLiteTest.Run();
            Console.WriteLine("任务外");
            Console.ReadKey();
        }

        private static void Test()
        {

            //Bitmap bitmap = new Bitmap(@"D:\Alvin\WoffFile\tyc-num-51a004c0a8.png");
            //UnCodebase unCodebase = new UnCodebase(bitmap);
            //unCodebase.BmpToBW();
            //string white_list = ".1234567890";
            //string defaultResult = OCRRecognize(unCodebase.bmpobj.Clone() as Bitmap, white_list);
            //string cubeResult = "";
            //if (!defaultResult.Contains("."))
            //{
            //    cubeResult = OCRRecognize(unCodebase.bmpobj.Clone() as Bitmap, white_list, EngineMode.CubeOnly);
            //    if (cubeResult.Contains("."))
            //    {
            //        int index = cubeResult.IndexOf('.');
            //        defaultResult = defaultResult.Insert(index, ".");
            //    }
            //}
            //unCodebase.bmpobj.Save(@"D:\Alvin\WoffFile\ceshi.png");
            //string result = "";
            //result+= Marshal.PtrToStringAnsi(OCR(@"D:\Alvin\WoffFile\1234567890.png", -1)).Replace("", "");
            //AspriseOCR.SetUp();
            //AspriseOCR ocr = new AspriseOCR();
            //ocr.StartEngine("eng", AspriseOCR.SPEED_FASTEST);

            //string s = ocr.Recognize(@"D:\Alvin\WoffFile\1234567890.png", -1, -1, -1, -1, -1, AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT);
            //Console.WriteLine("OCR Result: " + s);
            //string pattern = "[\u4e00-\u9fa5]+";

            //Match m =Regex.Match("adfjdjfeiof汉dfjedff地", pattern);

            //bool result = Regex.IsMatch("cddddddd", "^b");
            //Console.WriteLine(defaultResult);
            //Console.ReadKey();

            //HTMLDocumentClass 

            //Task.Factory.StartNew(() =>
            //{
            //    Thread.Sleep(5000);
            //    Console.WriteLine("任务内");
            //});
            //类似于委托的beginInvoke
            //Task task = new Task(OptionFun);
            //task.Start();

            //List<Task> tasks = new List<Task>();

            //for (int i = 0; i < 5; i++)
            //{
            //    tasks.Add(new Task(OptionFun));
            //}
            //Task.WaitAll(tasks.ToArray());

        }
        public static string OCRRecognize(Bitmap bitmap, string white_list = "", EngineMode engineModel = EngineMode.Default)
        {
            string rtn = string.Empty;
            using (var engine = new TesseractEngine(AppDomain.CurrentDomain.BaseDirectory + (@"/tessdata"), "eng", engineModel))
            {
                if (!string.IsNullOrEmpty(white_list)) engine.SetVariable("tessedit_char_whitelist", white_list);
                // have to load Pix via a bitmap since Pix doesn't support loading a stream.
                using (var image = bitmap)
                {
                    using (var pix = PixConverter.ToPix(image))
                    {
                        using (var page = engine.Process(pix, PageSegMode.SingleLine))
                        {
                            string meanConfidenceLabel = String.Format("{0:P}", page.GetMeanConfidence());

                            string resultText = page.GetText();
                            rtn = resultText;
                        }
                    }
                }
            }
            return rtn;
        }

        /*
        private static void SplitPDF()
        {
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("1453880297_894375.pdf");

            PdfDocument pdf1 = new PdfDocument();
            PdfPageBase page;
            for (int i = 0; i < 10; i++)
            {
                page = pdf1.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0));
                pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0));
            }
            pdf1.SaveToFile("DOC_1.pdf");

            PdfDocument pdf2 = new PdfDocument();
            for (int i = 10; i < 18; i++)
            {
                page = pdf2.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0));
                pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0));
            }
            pdf2.SaveToFile("DOC_2.pdf");
        }
        private static void GetPDFImg()
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("1453880297_894375.pdf");
            IList<Image> images = new List<Image>();
            foreach (PdfPageBase page in doc.Pages)
            {
                if (page.ExtractImages() != null)
                {
                    foreach (Image image in page.ExtractImages())
                    {
                        images.Add(image);
                    }
                }
            }
            doc.Close();
            int index = 0;
            foreach (Image image in images)
            {
                String imageFileName = String.Format("Image-{0}.png", index++);
                image.Save(imageFileName, ImageFormat.Png);
            }
        }
        public static int[] TwoSum(int[] nums, int target)
        {
            for (int i = 0; i < nums.Length - 1; i++)
            {
                for (int j = i + 1; j < nums.Length; j++)
                {
                    if (nums[i] + nums[j] == target)
                    {
                        return new int[] { i, j };
                    }
                }
            }
            return new int[] { 0, 0 };
        }

        private static int[] TwoSum(int[] nums, int target, int m, int n)
        {
            if (nums[m] + nums[n] == target)
            {
                return new int[] { m, n };
            }
            else if (nums[m] + nums[n] < target)
            {
                return TwoSum(nums, target, ++m, n);
            }
            else
            {
                return TwoSum(nums, target, m, --n);
            }
        }
        public static void OptionFun()
        {
            Thread.Sleep(2000);
            Console.WriteLine("任务内");
        }
        private static string FormatNameEN(string name)
        {
            name = name.ToLower();
            Regex reg = new Regex(@"[\w]+", RegexOptions.None);
            MatchCollection matches = reg.Matches(name);
            foreach (Match match in matches)
            {
                string temp = match.Value.Substring(0, 1).ToUpper() + match.Value.Substring(1, match.Length - 1);
                name = name.Replace(match.Value, temp);
            }
            return name;
        }
        */
    }
}
